﻿'
' Will Strohl (will.strohl@gmail.com)
' http://www.willstrohl.com
'
'Copyright (c) 2011, Will Strohl
'All rights reserved.
'
'Redistribution and use in source and binary forms, with or without modification, are 
'permitted provided that the following conditions are met:
'
'Redistributions of source code must retain the above copyright notice, this list of 
'conditions and the following disclaimer.
'
'Redistributions in binary form must reproduce the above copyright notice, this list 
'of conditions and the following disclaimer in the documentation and/or other 
'materials provided with the distribution.
'
'Neither the name of Will Strohl nor the names of its contributors may be 
'used to endorse or promote products derived from this software without specific prior 
'written permission.
'
'THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY 
'EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 
'OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT 
'SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, 
'INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED 
'TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR 
'BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 
'CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN 
'ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH 
'DAMAGE.
'

Imports DotNetNuke.Entities.Modules
Imports DotNetNuke.Services.Exceptions
Imports WillStrohl.Modules.CodeCamp.QueryStrings

Namespace WillStrohl.Modules.CodeCamp

    Partial Public MustInherit Class DefaultView
        Inherits WNSPortalModuleBase
        Implements IActionable

#Region " Private Members "

        Private Const c_MenuItem_EditCodeCamp As String = "ActionMenu.Item.EditCodeCamp"
        Private Const c_MenuItem_Tracks As String = "ActionMenu.Item.Tracks"
        Private Const c_MenuItem_Rooms As String = "ActionMenu.Item.Rooms"
        Private Const c_MenuItem_VolunteerTasks As String = "ActionMenu.Item.VolunteerTasks"
        Private Const c_MenuItem_OpenRegistration As String = "ActionMenu.Item.OpenRegistration"

        Private p_CodeCamp As CodeCampInfo = Nothing
        Private p_EditCodeCampUrl As String = Null.NullString

#End Region

#Region " Private Properties "

        Protected ReadOnly Property CodeCamp() As CodeCampInfo
            Get
                If Not Me.p_CodeCamp Is Nothing Then
                    Return Me.p_CodeCamp
                End If

                Dim ctlModule As New CodeCampController
                Me.p_CodeCamp = ctlModule.GetCodeCampByModule(Me.ModuleId)

                Return Me.p_CodeCamp
            End Get
        End Property

        Private ReadOnly Property EditCodeCampUrl As String
            Get
                If Not String.IsNullOrEmpty(Me.p_EditCodeCampUrl) Then
                    Return Me.p_EditCodeCampUrl
                End If

                If Me.CodeCamp Is Nothing Then
                    Me.p_EditCodeCampUrl = EditUrl(String.Empty, String.Empty, "EditCodeCamp")
                Else
                    Me.p_EditCodeCampUrl = EditUrl(String.Empty, String.Empty, "EditCodeCamp", String.Format("ccid={0}", Me.CodeCamp.CodeCampId))
                End If

                Return Me.p_EditCodeCampUrl
            End Get
        End Property

        Private ReadOnly Property TracksUrl As String
            Get
                Return EditUrl(String.Empty, String.Empty, "Tracks", String.Concat(CODE_CAMP_ID, "=", Me.CodeCamp.CodeCampId))
            End Get
        End Property

        Private ReadOnly Property RoomsUrl As String
            Get
                Return EditUrl(String.Empty, String.Empty, "Rooms")
            End Get
        End Property

        Private ReadOnly Property VolunteerTasksUrl As String
            Get
                Return EditUrl(String.Empty, String.Empty, "VolunteerTasks")
            End Get
        End Property

#End Region

#Region " Event Handlers "

        Private Sub Page_Load(ByVal Sender As Object, ByVal E As System.EventArgs) Handles Me.Load
            Try
                If Not Page.IsPostBack Then
                    Me.BindData()
                End If

                Me.RegisterClientScript("CodeCamp.Core", String.Concat(Me.ControlPath, "js/DNNCodeCamp.Core.js"))
            Catch exc As Exception ' Module failed to load
                ProcessModuleLoadException(Me, exc, Me.IsEditable)
            End Try
        End Sub

#Region " Checklist Navigation Events "

        Private Sub lnkCreateCodeCamp_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles lnkCreateCodeCamp.Click
            Response.Redirect(EditCodeCampUrl)
        End Sub

        Private Sub lnkCreateTracks_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles lnkCreateTracks.Click
            Response.Redirect(TracksUrl)
        End Sub

        Private Sub lnkCreateRooms_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles lnkCreateRooms.Click
            Response.Redirect(RoomsUrl)
        End Sub

        Private Sub lnkVolunteerTasks_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles lnkVolunteerTasks.Click
            Response.Redirect(VolunteerTasksUrl)
        End Sub

        Private Sub lnkOpenRegistration_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles lnkOpenRegistration.Click
            Dim ctlCodeCamp As New CodeCampController
            ctlCodeCamp.EnableCodeCamp(Me.CodeCamp.CodeCampId, Not Me.CodeCamp.RegistrationActive)
        End Sub

#End Region

#End Region

#Region " Helper Methods "

        Private Sub BindData()

            Me.LocalizeModule()

            Me.pnlCodeCamp.Visible = (Not Me.CodeCamp Is Nothing)
            Me.pnlCheckList.Visible = (Me.CodeCamp Is Nothing And Me.IsEditable)
            Me.pnlNoCodeCamp.Visible = (Me.CodeCamp Is Nothing And Not Me.IsEditable)

            Me.lnkCreateCodeCamp.Enabled = (Me.CodeCamp Is Nothing OrElse Me.CodeCamp.CodeCampId < 1)

            If Not Me.CodeCamp Is Nothing Then
                Dim ctlCodeCamp As New CodeCampController

                Me.lnkCreateTracks.Enabled = (Not Me.CodeCamp Is Nothing AndAlso ctlCodeCamp.CodeCampTrackCount(Me.CodeCamp.CodeCampId) < 1)
                Me.lnkCreateRooms.Enabled = (Not Me.CodeCamp Is Nothing AndAlso ctlCodeCamp.CodeCampRoomCount(Me.CodeCamp.CodeCampId) < 1)
                Me.lnkVolunteerTasks.Enabled = (Not Me.CodeCamp Is Nothing AndAlso ctlCodeCamp.CodeCampVolunteerTaskCount(Me.CodeCamp.CodeCampId) < 1)
                Me.lnkOpenRegistration.Enabled = (Not Me.CodeCamp Is Nothing AndAlso Not Me.CodeCamp.RegistrationActive)
            Else
                Me.lnkCreateTracks.Enabled = False
                Me.lnkCreateRooms.Enabled = False
                Me.lnkVolunteerTasks.Enabled = False
                Me.lnkOpenRegistration.Enabled = False
            End If

            If Not Me.CodeCamp Is Nothing Then
                Me.pnlCheckList.Visible = (Me.IsEditable And (Me.lnkCreateCodeCamp.Enabled Or Me.lnkCreateTracks.Enabled Or Me.lnkCreateRooms.Enabled Or Me.lnkVolunteerTasks.Enabled Or Me.lnkOpenRegistration.Enabled))
            End If

        End Sub

        Private Sub LocalizeModule()

            Me.lnkCreateCodeCamp.Text = Me.GetLocalizedString("Task.CreateCodeCamp.Text")
            Me.lnkCreateTracks.Text = Me.GetLocalizedString("Task.CreateTracks.Text")
            Me.lnkCreateRooms.Text = Me.GetLocalizedString("Task.CreateRooms.Text")
            Me.lnkVolunteerTasks.Text = Me.GetLocalizedString("Task.VolunteerTasks.Text")
            Me.lnkOpenRegistration.Text = Me.GetLocalizedString("Task.OpenRegistration.Text")

        End Sub

#End Region

#Region " IActionable Implementation "

        Public ReadOnly Property ModuleActions() As DotNetNuke.Entities.Modules.Actions.ModuleActionCollection Implements IActionable.ModuleActions
            Get
                Dim Actions As New Actions.ModuleActionCollection

                Actions.Add(GetNextActionID, Me.GetLocalizedString(c_MenuItem_EditCodeCamp), _
                    String.Empty, String.Empty, String.Empty, _
                    Me.EditCodeCampUrl, _
                    False, DotNetNuke.Security.SecurityAccessLevel.Edit, True, False)

                If Not Me.CodeCamp Is Nothing AndAlso Me.CodeCamp.CodeCampId > 0 Then

                    Actions.Add(GetNextActionID, Me.GetLocalizedString(c_MenuItem_Tracks), _
                        String.Empty, String.Empty, String.Empty, _
                        Me.TracksUrl, _
                        False, DotNetNuke.Security.SecurityAccessLevel.Edit, True, False)

                    Actions.Add(GetNextActionID, Me.GetLocalizedString(c_MenuItem_Rooms), _
                        String.Empty, String.Empty, String.Empty, _
                        Me.RoomsUrl, _
                        False, DotNetNuke.Security.SecurityAccessLevel.Edit, True, False)

                    Actions.Add(GetNextActionID, Me.GetLocalizedString(c_MenuItem_VolunteerTasks), _
                        String.Empty, String.Empty, String.Empty, _
                        Me.VolunteerTasksUrl, _
                        False, DotNetNuke.Security.SecurityAccessLevel.Edit, True, False)

                    If Not Me.CodeCamp.RegistrationActive Then
                        Actions.Add(GetNextActionID, Me.GetLocalizedString(c_MenuItem_OpenRegistration), _
                            "lnkOpenRegistration_Click", String.Empty, String.Empty, _
                            Me.VolunteerTasksUrl, _
                            False, DotNetNuke.Security.SecurityAccessLevel.Edit, True, False)
                    End If

                End If

                Return Actions
            End Get
        End Property

#End Region

    End Class

End Namespace